package ewms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import ewms.bo.EwmsWarehouseAddBo;
import ewms.bo.EwmsWarehouseEditBo;
import ewms.bo.EwmsWarehouseQueryBo;
import ewms.common.utils.PageUtils;
import ewms.common.core.page.PagePlus;
import ewms.common.core.page.TableDataInfo;
import ewms.domain.EwmsWarehouse;
import ewms.mapper.EwmsWarehouseMapper;
import ewms.service.IEwmsWarehouseService;
import ewms.vo.EwmsWarehouseVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 仓库信息Service业务层处理
 *
 * @author zhaoxiaochun
 * @date 2022-01-11
 */
@Service
public class EwmsWarehouseServiceImpl extends ServiceImpl<EwmsWarehouseMapper, EwmsWarehouse> implements IEwmsWarehouseService {

    @Override
    public EwmsWarehouseVo queryById(Long warehouseId){
        return getVoById(warehouseId, EwmsWarehouseVo.class);
    }

    @Override
    public TableDataInfo<EwmsWarehouseVo> queryPageList(EwmsWarehouseQueryBo bo) {
        PagePlus<EwmsWarehouse, EwmsWarehouseVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), EwmsWarehouseVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<EwmsWarehouseVo> queryList(EwmsWarehouseQueryBo bo) {
        return listVo(buildQueryWrapper(bo), EwmsWarehouseVo.class);
    }

    private LambdaQueryWrapper<EwmsWarehouse> buildQueryWrapper(EwmsWarehouseQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EwmsWarehouse> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getWarehouseName()), EwmsWarehouse::getWarehouseName, bo.getWarehouseName());
        lqw.eq(StrUtil.isNotBlank(bo.getWarehouseType()), EwmsWarehouse::getWarehouseType, bo.getWarehouseType());
        lqw.like(StrUtil.isNotBlank(bo.getWarehouseAddress()), EwmsWarehouse::getWarehouseAddress, bo.getWarehouseAddress());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(EwmsWarehouseAddBo bo) {
        EwmsWarehouse add = BeanUtil.toBean(bo, EwmsWarehouse.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(EwmsWarehouseEditBo bo) {
        EwmsWarehouse update = BeanUtil.toBean(bo, EwmsWarehouse.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(EwmsWarehouse entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
